package com.udaos.modules.company.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.annotation.SLog;
import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.company.Ud_company;
import com.udaos.models.sys.Sys_user;
import com.udaos.modules.company.services.UdCompanyService;


/**
 * 主体WEB层
 * @author mutaotao
 * @time 2016-11-07 17:19:18
 * 
 */
@IocBean
@At("/platform/company")
@Filters({ @By(type = PrivateFilter.class)})
public class UdCompanyController {
	
	@Inject
	private UdCompanyService udCompanyService;


	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/base/company/index.html")
	@RequiresAuthentication
	public void index() {

	}
	/**
	 * 加载数据
	 * @param name   名称
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("name") String name,@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		Cnd cnd = Cnd.NEW();
		cnd.and("delFlag", "=", "0");
		cnd.desc("createAt");
		if (!Strings.isBlank(name))
			cnd.and("name", "like", "%" + name + "%");
		return udCompanyService.data(length, start, draw, order, columns, cnd, null);
	}
	
	/**
	 * 新增
	 * @param unitid
	 * @return
	 */
	@At
	@Ok("beetl:/platform/base/company/add.html")
	@RequiresAuthentication
	public void add() {
		
	}
	/**
	 * 新增
	 * @param company
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	@SLog(tag = "新增主体", msg = "主体名:${args[0].name}")
	public Object addDo(@Param("..") Ud_company company, HttpServletRequest req) {
		try {
			//设置创建时间
			company.setCreateAt((int)(System.currentTimeMillis()/1000));
			//设置加入时间
			company.setJoin_date((int)(System.currentTimeMillis()/1000));
			//设置创建人
			Subject subject = SecurityUtils.getSubject();
            Sys_user user = (Sys_user) subject.getPrincipal();
            company.setCreateBy(user.getId());
			udCompanyService.insert(company);
	        return Result.success("保存成功");
	    } catch (Exception e) {
	    	return Result.error(e.getMessage());
	    }
	}
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@At("/edit/?")
	@Ok("beetl:/platform/base/company/edit.html")
	@RequiresAuthentication
	public Object edit(String id) {
		return udCompanyService.fetch(id);
	}
	
	/**
	 * 修改
	 * @param member
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	@SLog(tag = "修改主体", msg = "主体名:${args[0].name}")
	public Object editDo(@Param("..") Ud_company company, HttpServletRequest req) {
		try {
			//设置修改
			company.setOpAt((int)(System.currentTimeMillis()/1000));
			udCompanyService.updateIgnoreNull(company);
	   		return Result.success("保存成功");
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}
	
	/**
	 * 删除
	 * @param id
	 * @param req
	 * @return
	 */
	@At("/delete/?")
    @Ok("json")
	@RequiresAuthentication
    @SLog(tag = "删除主体", msg = "主体名:${args[1].getAttribute('name')}")
    public Object delete(String id, HttpServletRequest req) {
        try {
        	Ud_company company = udCompanyService.fetch(id);
        	company.setDelFlag(true);
        	udCompanyService.updateIgnoreNull(company);
            req.setAttribute("name", company.getName());
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}